﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CliffPusher
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            ucPlayerLocation1.Players.Add(new ucPlayerLocation.PlayerTokenStack(Color.Red, Color.Wheat) { Count = 3 } );
            ucPlayerLocation1.Players.Add(new ucPlayerLocation.PlayerTokenStack(Color.Blue, Color.White) { Count = 1 });
            ucPlayerLocation1.Players.Add(new ucPlayerLocation.PlayerTokenStack(Color.Green) { Count = 4 });
            ucPlayerLocation1.Players.Add(new ucPlayerLocation.PlayerTokenStack(Color.Yellow, Color.Black) { Count = 2 });
            ucPlayerLocation1.MaxPlayers = 4;
            ucPlayerLocation1.HorizontalLocations = 2;

            AllControls(this)
                .OfType<ucPlayerLocation>()
                .Where(uc => uc != ucPlayerLocation1)
                .ToList()
                .ForEach(uc=>
                {
                    var newPlayers = ucPlayerLocation1
                        .Players
                        .Select(p => new ucPlayerLocation.PlayerTokenStack(p.TextColor) { BackColor = p.BackColor, Count = p.Count });
                    uc.Players.AddRange(newPlayers);
                    uc.MaxPlayers = ucPlayerLocation1.MaxPlayers;
                    uc.HorizontalLocations = ucPlayerLocation1.HorizontalLocations;
                });

            AllControls(this)
                .OfType<ucPlayerCard>()
                .ToList()
                .ForEach(uc=> uc.PlayingCard += Uc_PlayingCard);
        }

        private IEnumerable<Control> AllControls( Control control )
        {
            foreach(Control ctl in control.Controls )
            {
                yield return ctl;
                foreach(Control sub in AllControls(ctl) )
                {
                    yield return sub;
                }
            }
        }

        private bool Uc_PlayingCard(ucPlayerCard arg)
        {
            var msg = $"You have played '{arg.CardTitle}'";
            MessageBox.Show(msg);
            return false;
        }

        ucPlayerLocation.PlayerTokenStack CurrentPlayer
        {
            get
            {
                var names = ucPlayerLocation1.Players.Select(p => p.BackColor.Name).ToList();
                var player = ucPlayerLocation1.Players.FirstOrDefault(p => p.BackColor.Name == comboBox1.SelectedItem.ToString());
                return player;
            }
        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            var player = CurrentPlayer;
            if (player != null)
            {
                player.Count = (int)numericUpDown1.Value;
            }
        }

        GameEngine.Engine _engine;
        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.Items.Clear();
            comboBox1.Items.AddRange(ucPlayerLocation1.Players.Select(p => p.BackColor.Name).ToArray());

            _engine = new CliffPusherEngine();
            //_engine.Players.Add(new GameEngine.Players.HumanPlayer("You"));
            _engine.Players.Add(new CliffCrowds.CliffCrowdsAIPlayer("Randy"));
            _engine.Players.Add(new CliffCrowds.CliffCrowdsAIPlayer("Predictor"));
            _engine.Start();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            var player = CurrentPlayer;
            if (player != null)
            {
                numericUpDown1.Value = player.Count;
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            _engine?.Stop(true);
        }

        /*
        private void toolTip1_Popup(object sender, PopupEventArgs e)
        {
            var uc = e.AssociatedControl as ucPlayerCard;
            if( uc != null )
            {
                // toolTip1.ToolTipTitle = uc.CardName;
            }
        }
        */
    }
}
